package com.ruoyi.web.controller.custom;

import com.ruoyi.custom.domain.Bill;
import com.ruoyi.custom.service.IBillService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.ruoyi.common.core.domain.AjaxResult.Type.WARN;

/**
 * 账单管理Controller
 * 
 * @author wule
 * @date 2024-04-08
 */
@Controller
@RequestMapping("/custom/bill")
public class BillController extends BaseController
{
    private String prefix = "custom/bill";

    @Autowired
    private IBillService billService;

    @RequiresPermissions("custom:bill:view")
    @GetMapping()
    public String bill()
    {
        return prefix + "/bill";
    }

    /**
     * 查询账单管理列表
     */
    @RequiresPermissions("custom:bill:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Bill bill)
    {
        startPage();
        List<Bill> list = billService.selectBillList(bill);
        return getDataTable(list);
    }

    /**
     * 导出账单管理列表
     */
    @RequiresPermissions("custom:bill:export")
    @Log(title = "账单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Bill bill)
    {
        List<Bill> list = billService.selectBillList(bill);
        ExcelUtil<Bill> util = new ExcelUtil<Bill>(Bill.class);
        return util.exportExcel(list, "账单管理数据");
    }

    /**
     * 新增账单管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存账单管理
     */
    @RequiresPermissions("custom:bill:add")
    @Log(title = "账单管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Bill bill)
    {
        int i = billService.insertBill(bill);
        if (i==0||i==1){
            return toAjax(i);
        }else {
            return error(WARN,"该用户余额不足,请先充值!");
        }
    }

    /**
     * 修改账单管理
     */
    @RequiresPermissions("custom:bill:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Bill bill = billService.selectBillById(id);
        mmap.put("bill", bill);
        return prefix + "/edit";
    }

    /**
     * 修改保存账单管理
     */
    @RequiresPermissions("custom:bill:edit")
    @Log(title = "账单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Bill bill)
    {
        return toAjax(billService.updateBill(bill));
    }

    /**
     * 删除账单管理
     */
    @RequiresPermissions("custom:bill:remove")
    @Log(title = "账单管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(billService.deleteBillByIds(ids));
    }
}
